var express = require('express');
var router = express.Router();
var StaticizeUtil = require('../../common/util/StaticizeUtil');
var path=require('path');
var CONFIG = require('../../common/config');

//------------------------------------------------------------------
/* 注意：/ck 为本服务健康检查路径，不可更改. */
router.get('/ck', function(req, res, next) {
  res.json({'ok':':)'});
});
//------------------------------------------------------------------
/* GET home page. */
//router.get('/', function(req, res, next) {
//
//    var data = {id: 1, name: "jack", child: {name: "llc", age: 2}};
//    data.child.toString = function () {
//        return JSON.stringify(this);
//    };
//    res.render('index', { title: 'Api',success:1,msg:"ok",data:data});
//}


//------------------------------------------------------------------
/* 清除静态化的数据 */
router.get('/clearStaticize', function(req, res, next) {
    StaticizeUtil.clearDataByRedis();
    res.json({'ok':':)'});
});

//------------------------------------------------------------------
/* GET home page. */
router.get('/', function(req, res, next) {
    // 从缓存中获取数据，能获取到就直接返回
    var uri='/';
    var p = {a:1};
    StaticizeUtil.getByRedis(__filename, uri, p, function(error, content){
        if(content){
            return res.end(content);
        }

        var data = {id:1,name:"jack",child:{name:"llc",age:2}};
        data.child.toString = function(){ return JSON.stringify(this);};
        data = { title: 'Api',success:1,msg:"ok",data:data};

        // 渲染jade页面为html，并纪录数据到缓存中
        res.render('index', { title: 'Api',success:1,msg:"ok",data:data}, function(error, data){
            var content = StaticizeUtil.recordByRedis(__filename,   uri, p,data);
            res.end(content);
        });
    });


});
//------------------------------------------------------------------



module.exports = router;
